package com.gx.security.Symmetric_encryption;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import java.security.*;
import java.security.SecureRandom;

/**
 * Created by gx on 2017/3/28.
 */
public class DESTest {
    private static String src = "imooc security DES";


    public static void main(String[] args) {
        jdkDES();
        bcDES();
    }

    public static void jdkDES() {
        try {
            //初始化key  生成器
            KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
            //初始化key生成长度
            keyGenerator.init(56);
            SecretKey secretKey = keyGenerator.generateKey();
            //            拿到key
            byte[] byteskey = secretKey.getEncoded();

//            如果要指定 key  以上代码去掉    初始化DESKeySpec 中参数为 key的字节数组
//            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());


            DESKeySpec desKeySpec = new DESKeySpec(byteskey);
//            实例化key工厂
//             输入参数指定加密方式
            SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
//            获得转化之后的key
            Key convertSecretKey = factory.generateSecret(desKeySpec);
            //加密                          //加解密算法/工作模式/数据填充方式
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            //加密模式           转化后key值
            cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
            //执行加密
            byte[] result = cipher.doFinal(src.getBytes());
//                                                            可以16进制也可以base64
            System.out.println("jdk des 加密结果encrypt:" + Hex.encodeHexString(result));

            //解密操作       机密模式
            cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);


            //指定key解密
//                 // 创建一个DESKeySpec对象
//                DESKeySpec desKey = new DESKeySpec(src.getBytes());
//                // 创建一个密匙工厂
//                SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
//             // 将DESKeySpec对象转换成SecretKey对象
//                SecretKey securekey = keyFactory.generateSecret(desKey);
            //            拿到原始的result 解密


            result = cipher.doFinal(Hex.decodeHex(Hex.encodeHex(result)));
            System.out.println("jdk des 解密结果decrypt:" + new String(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void bcDES() {

        try {
            Security.addProvider(new BouncyCastleProvider());

            //初始化key  生成器                                          指定Provider(提供者)
            KeyGenerator keyGenerator = KeyGenerator.getInstance("DES", "BC");
            //初始化key生成长度
            keyGenerator.init(56);
            SecretKey secretKey = keyGenerator.generateKey();
            //            拿到key
            byte[] byteskey = secretKey.getEncoded();
            DESKeySpec desKeySpec = new DESKeySpec(byteskey);
//            实例化key工厂
//             输入参数指定加密方式
            SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
//            获得转化之后的key
            Key convertSecretKey = factory.generateSecret(desKeySpec);
            //加密                          //加解密算法/工作模式/数据填充方式
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            //加密模式           转化后key值
            cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
            //执行加密
            byte[] result = cipher.doFinal(src.getBytes());
//                                                            可以16进制也可以base64
            System.out.println("bc des 加密结果encrypt:" + Hex.encodeHexString(result));

            //解密操作       机密模式
            cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);

//            拿到原始的result 解密
            result = cipher.doFinal(result);
            System.out.println("bc des 解密结果decrypt:" + new String(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    public static void ccDES(){
//        DigestUtils
//    }


}
